#import "CTAFManager.h"
#import <AppsFlyerLib/AppsFlyerLib.h>
#import <AdSupport/AdSupport.h>
@interface CTAFManager () <AppsFlyerLibDelegate>
@end
@implementation CTAFManager
static CTAFManager *_instance = nil;
+ (instancetype)shared {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[CTAFManager alloc] init];
    });
    return _instance;
}
- (instancetype)init {
    if (self = [super init]) {
        [AppsFlyerLib shared].appsFlyerDevKey = kAFDevKey;
        [AppsFlyerLib shared].appleAppID = kAFAppid;
        [AppsFlyerLib shared].delegate = self;
        [[AppsFlyerLib shared] waitForATTUserAuthorizationWithTimeoutInterval:60];
    }
    return self;
}
+ (void)logEvent:(NSString *)event {
    [[AppsFlyerLib shared] logEvent:event withValues:@{}];
}
+ (NSString *)getChannel{
    [CTAFManager shared];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *channel=(NSString *)[defaults objectForKey:@"k_Channel_key"];
    if (channel != nil && channel.length > 0) {
        return channel;
    }
    return kChannelDefault;
}
-(void)onConversionDataSuccess:(NSDictionary*) installData {
    id status = [installData objectForKey:@"af_status"];
    if([status isEqualToString:@"Non-organic"]) {
        NSString *channel = [installData objectForKey:@"campaign"];
        [self setNewChannel:channel dic:installData];
    } else if([status isEqualToString:@"Organic"]) {
        [self setStr:kChannelOrganic key:@"k_Channel_key"];
    }
    [self uploadChData];
}
- (void)uploadChData{
    if (beNil([CTUserManager shareManager].userModel.userGroup)) {
        return;
    }
    [CTUploadManager ct_uploadType:(CTUploadTypeUpdateRegSubChannel) data:@{}];
    [CTUploadManager ct_uploadType:CTUploadTypeInstalls];
}
-(void)onConversionDataFail:(NSError *) error {
    NSLog(@"onConversionDataFail  --  %@",error);
}
- (void)onAppOpenAttribution:(NSDictionary*)attributionData {
}
- (void)setNewChannel:(NSString *)channel dic:(NSDictionary *)dic {
    if (channel != nil && channel.length > 0) {
        NSString *resultChannel = nil;
        resultChannel = [self getAbsoluteChFromCampaign:channel];
        if ([CTAFManager isEmpty:resultChannel]) {
            resultChannel = [dic objectForKey:@"af_channel"];
        }
        if (resultChannel != nil && resultChannel.length > 0) {
            [self setStr:resultChannel key:@"k_Channel_key"];
        }
    }
}
- (NSString *)getAbsoluteChFromCampaign:(NSString *)campaign{
    NSString * absoluteCh = @"";
    NSArray *channelArray = @[kChannelPre1,kChannelPre2];
    for (NSString *ch in channelArray) {
        if ([campaign containsString:ch]) {
            @try {
                NSRange range = [campaign rangeOfString:ch];
                NSUInteger index = range.location;
                absoluteCh = [campaign substringWithRange:NSMakeRange(index, 10)];
            } @catch (NSException *exception) {
                absoluteCh = nil;
            } @finally {
            }
            return absoluteCh;
        }
    }
    return absoluteCh;
}
- (void)setStr:(NSString *)str key:(NSString *)key{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:str forKey:key];
    [defaults synchronize];
}
+ (BOOL)isEmpty:(NSString *)str {
    if (!str) {
        return YES;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return YES;
        } else {
            return NO;
        }
    }
}
@end
